home *** CD-ROM | disk | FTP | other *** search
- Path: Rezonet.net!news
- From: ray@ultimate-tech.com (Ray Dunn)
- Newsgroups: comp.lang.c
- Subject: Re: Please help ?!
- Date: 25 Jan 1996 00:11:58 GMT
- Organization: Ultimate Technographics Inc.
- Message-ID: <4e6hse$dvl@ns.RezoNet.NET>
- References: <4dm889$3hs@neptunus.pi.net> <4drnv1$cr@news.iag.net> <4drq5i$cr@news.iag.net>
- NNTP-Posting-Host: 204.19.230.7
- Mime-Version: 1.0
- Content-Type: Text/Plain; charset=US-ASCII
- X-Newsreader: WinVN 0.99.7
-
- In referenced article, John R Buchan says...
- >> cpy = (char *) malloc(MAXLEN);
- >
- >The cast is unnecessary and can hide errors. You should remove it.
-
- This is the second time I've seen this advice in the last five minutes
- in the newsgroup.
-
- Certainly the cast is unnecessary, and certainly *some* unnecessary
- casts can hide errors, but casting the result of malloc can *never*
- hide an error, because it's telling the compiler to do something it
- would do anyway.
-
- On the other hand, if by any chance "cpy" is not a "char *", but you
- thought it was when you wrote the statement, then including the cast
- will *catch* an error.
-
- I'd go as far as to say that if the argument of malloc, calloc,
- etc. contains a sizeof operator such that a number of elements of that
- type are being allocated, then adding a cast to a pointer of the same
- type to the malloc return is the *safest* thing you can do:
-
- fred = malloc(n * sizeof(int));
-
- Oops - fred isn't an "int *" it's a "long *", but the compiler wont
- issue any warnings, but in:
-
- fred = (int *)malloc(n * sizeof(int));
-
- the compiler will issue an error.
-
- --
- Ray Dunn (opinions are my own) | Phone: (514) 938 9050
- Montreal | Phax : (514) 938 5225
- ray@ultimate-tech.com | Home : (514) 630 3749
-
-